package he.widget.toast;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Paint;
import android.graphics.drawable.Animatable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.RoundRectShape;
import android.support.annotation.AnimRes;
import android.support.annotation.ColorInt;
import android.support.annotation.DrawableRes;
import android.support.annotation.FloatRange;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.AppCompatImageView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.TextView;
import android.widget.Toast;


@SuppressWarnings("SameParameterValue")
public class HToast {

    public static void Loading(Context context, @NonNull CharSequence msg) {

        final Toast toast = new Toast(context);
        setToastDuration(toast, msg);

        View v = new ToastViewBuilder(context)
            .setMessage(msg)
            .setBackGround(context.getResources().getColor(R.color.md_black_1000), null)
            .setIconResource(R.drawable.ic_sync_black_24dp)
            .setIconAnim(R.anim.rotate)
            .getView();

        toast.setView(v);
        toast.show();
    }

    public static void success(Context context, @NonNull CharSequence msg) {
        final Toast toast = new Toast(context);
        setToastDuration(toast, msg);

        View v = new ToastViewBuilder(context)
            .setMessage(msg)
            .setBackGround(context.getResources().getColor(R.color.md_green_500), null)
            .setIconResource(R.drawable.anim_ic_done)
            .getView();

        toast.setView(v);
        toast.show();
    }

    public static void warning(Context context, @NonNull CharSequence msg) {
        final Toast toast = new Toast(context);
        setToastDuration(toast, msg);

        View v = new ToastViewBuilder(context)
            .setMessage(msg)
            .setBackGround(context.getResources().getColor(R.color.md_orange_700), null)
            .setIconResource(R.drawable.ic_error_outline_black_24dp)
            .getView();

        toast.setView(v);
        toast.show();
    }

    public static void error(Context context, @NonNull CharSequence msg) {
        final Toast toast = new Toast(context);
        setToastDuration(toast, msg);

        View v = new ToastViewBuilder(context)
            .setMessage(msg)
            .setBackGround(context.getResources().getColor(R.color.md_red_700), null)
            .setIconResource(R.drawable.ic_error_outline_black_24dp)
            .getView();

        toast.setView(v);
        toast.show();
    }


    private static void setToastDuration(Toast toast, @NonNull CharSequence msg) {
        toast.setDuration(msg.length() > 10 ? Toast.LENGTH_LONG : Toast.LENGTH_SHORT);
    }


    @SuppressWarnings("WeakerAccess")
    public static class ToastViewBuilder {

        private Context context;
        private View view;

        public ToastViewBuilder(Context context) {
            this.context = context;
            createView();
        }

        @SuppressLint("InflateParams")
        protected void createView() {
            view = LayoutInflater.from(context).inflate(R.layout.h_toast, null);
        }

        @SuppressWarnings("unused")
        public ToastViewBuilder setBackGroundResource(@DrawableRes int resid) {
            view.setBackgroundResource(resid);
            return this;
        }

        public ToastViewBuilder setBackGround(@ColorInt int color, @Nullable float[] radii) {

            if (radii != null) {
                if (radii.length < 4) {
                    throw new ArrayIndexOutOfBoundsException("radii must have 4 values");
                }

                radii = new float[]{radii[0], radii[0], radii[1], radii[1], radii[2], radii[2], radii[3], radii[3]};
            } else {
                // 在 android 4.1 设置一个大的值会变成椭圆形
                // 在 android 4.4 以上是胶囊形状
                // radii = new float[]{9999, 9999, 9999, 9999, 9999, 9999, 9999, 9999};

                final float scale = context.getResources().getDisplayMetrics().density;
                final float r = (int) (24 * scale + 0.5f); // 24dp
                radii = new float[]{r, r, r, r, r, r, r, r};
            }


            RoundRectShape shape = new RoundRectShape(radii, null, null);
            ShapeDrawable drawable = new ShapeDrawable(shape);
            drawable.getPaint().setColor(color);
            drawable.getPaint().setStyle(Paint.Style.FILL);


            view.setBackground(drawable);
            return this;
        }

        @SuppressWarnings("unused")
        public ToastViewBuilder setAlpha(@FloatRange(from = 0.0, to = 1.0) float alpha) {
            view.setAlpha(alpha);
            return this;
        }

        public ToastViewBuilder setMessage(CharSequence message) {
            ((TextView) view.findViewById(R.id.message)).setText(message);
            return this;
        }

        public ToastViewBuilder setIconResource(@DrawableRes int resId) {

            final AppCompatImageView imageView = view.findViewById(R.id.icon);
            imageView.setImageResource(resId);

            //play vector anim
            Drawable drawable = imageView.getDrawable();
            if (drawable instanceof Animatable) ((Animatable) drawable).start();

            return this;
        }

        public ToastViewBuilder setIconAnim(@AnimRes int id) {
            final AppCompatImageView imageView = view.findViewById(R.id.icon);
            final Animation anim = AnimationUtils.loadAnimation(context, id);
            imageView.startAnimation(anim);
            return this;
        }

        public View getView() {
            return view;
        }

    }

}
